package com.ruoyi.stock.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock.mapper.StockInventoryDetailMapper;
import com.ruoyi.stock.domain.StockInventoryDetail;
import com.ruoyi.stock.service.IStockInventoryDetailService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.basedata.service.IBaseProductService;
import com.ruoyi.basedata.domain.BaseProduct;
import com.ruoyi.stock.service.IStockInventoryCheckService;
import com.ruoyi.stock.domain.StockInventoryCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 库存盘点明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Service
public class StockInventoryDetailServiceImpl implements IStockInventoryDetailService 
{
    private static final Logger log = LoggerFactory.getLogger(StockInventoryDetailServiceImpl.class);
    
    @Autowired
    private StockInventoryDetailMapper stockInventoryDetailMapper;
    
    @Autowired
    private IBaseProductService baseProductService;
    
    @Autowired
    private IStockInventoryCheckService stockInventoryCheckService;

    /**
     * 查询库存盘点明细
     * 
     * @param detailId 库存盘点明细主键
     * @return 库存盘点明细
     */
    @Override
    public StockInventoryDetail selectStockInventoryDetailByDetailId(Long detailId)
    {
        return stockInventoryDetailMapper.selectStockInventoryDetailByDetailId(detailId);
    }

    /**
     * 查询库存盘点明细列表
     * 同时获取关联的商品名称和盘点单号信息
     * 
     * @param stockInventoryDetail 库存盘点明细
     * @return 库存盘点明细集合
     */
    @Override
    public List<StockInventoryDetail> selectStockInventoryDetailList(StockInventoryDetail stockInventoryDetail)
    {
        List<StockInventoryDetail> detailList = stockInventoryDetailMapper.selectStockInventoryDetailList(stockInventoryDetail);
        
        // 遍历明细列表，设置商品名称和盘点单号
        for (StockInventoryDetail detail : detailList) {
            // 获取商品名称
            try {
                Long productId = detail.getProductId();
                if (productId != null) {
                    BaseProduct product = baseProductService.selectBaseProductByProductId(productId);
                    if (product != null && product.getProductName() != null) {
                        // 将商品名称临时存储在前端展示字段中
                        detail.setRemark(product.getProductName());
                    }
                }
            } catch (Exception e) {
                log.error("获取商品名称失败", e);
            }
            
            // 获取盘点单号
            try {
                Long checkId = detail.getCheckId();
                if (checkId != null) {
                    StockInventoryCheck check = stockInventoryCheckService.selectStockInventoryCheckByCheckId(checkId);
                    if (check != null && check.getCheckCode() != null) {
                        // 将盘点单号临时存储在前端展示字段中
                        detail.setCreatedBy(check.getCheckCode());
                    }
                }
            } catch (Exception e) {
                log.error("获取盘点单号失败", e);
            }
        }
        
        return detailList;
    }

    /**
     * 新增库存盘点明细
     * 
     * @param stockInventoryDetail 库存盘点明细
     * @return 结果
     */
    @Override
    public int insertStockInventoryDetail(StockInventoryDetail stockInventoryDetail)
    {
        return stockInventoryDetailMapper.insertStockInventoryDetail(stockInventoryDetail);
    }

    /**
     * 修改库存盘点明细
     * 
     * @param stockInventoryDetail 库存盘点明细
     * @return 结果
     */
    @Override
    public int updateStockInventoryDetail(StockInventoryDetail stockInventoryDetail)
    {
        return stockInventoryDetailMapper.updateStockInventoryDetail(stockInventoryDetail);
    }

    /**
     * 批量删除库存盘点明细
     * 
     * @param detailIds 需要删除的库存盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryDetailByDetailIds(String detailIds)
    {
        return stockInventoryDetailMapper.deleteStockInventoryDetailByDetailIds(Convert.toStrArray(detailIds));
    }

    /**
     * 删除库存盘点明细信息
     * 
     * @param detailId 库存盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryDetailByDetailId(Long detailId)
    {
        return stockInventoryDetailMapper.deleteStockInventoryDetailByDetailId(detailId);
    }
}
